import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/7/1 21:22
 */
// 对称二叉树
    // 迭代   双队列
public class Solution101 {
    public boolean isSymmetric(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        deque.addFirst(root.left);
        deque.addLast(root.right);
        while (!deque.isEmpty()) {
            TreeNode leftNode = deque.pollFirst();
            TreeNode rightNode = deque.pollLast();
            if (leftNode == null && rightNode == null) {
                continue;
            }
            if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
                return false;
            }

            deque.addFirst(leftNode.left);
            deque.addLast(rightNode.right);
            deque.addFirst(leftNode.right);
            deque.addLast(rightNode.left);
        }
        return true;
    }
}
